const UserService = require('../services/UserService')
const JWT = require('../util/JWT')

const UserController = {
  login: async (req, res) => {
    // console.log(req.body)
    const result = await UserService.login(req.body)
    if (result.length === 0) {
      res.send({
        code: -1,
        error: '用户名密码不匹配'
      })
    } else {
      // 生成token并且设置在Header里
      const token = JWT.generate(
        {
          _id: result[0]._id,
          username: result[0].username
        },
        '1d'
      )
      res.header('Authorization', token)
      res.send({
        ActionType: 'OK',
        data: {
          username: result[0].username,
          name: result[0].name,
          gender: result[0].gender,
          role: result[0].role,
          phone: result[0].phone,
          email: result[0].email,
          avatar: result[0].avatar
        }
      })
    }
  },

  upload: async (req, res) => {
    // console.log(req.body, req.file)
    const { phone, email } = req.body
    const avatar = req.file ? `/avataruploads/${req.file.filename}` : ''
    const token = req.headers['authorization'].split(' ')[1]
    const payload = JWT.verify(token)

    await UserService.upload({ _id: payload._id, phone: Number(phone), email: Number(email), avatar })
    if (avatar) {
      res.send({
        ActionType: 'OK',
        data: {
          phone: Number(phone),
          email,
          avatar
        }
      })
    } else {
      res.send({
        ActionType: 'OK',
        data: {
          phone: Number(phone),
          email
        }
      })
    }
  },

  add: async (req, res) => {
    const { username, password, name, gender, role, phone, email } = req.body
    const avatar = req.file ? `/avataruploads/${req.file.filename}` : ''

    await UserService.add({ username: Number(username), password, name, gender: Number(gender), role: Number(role), phone: Number(phone), email, avatar })
    res.send({
      ActionType: 'OK'
    })
  },

  getList: async (req, res) => {
    const page = +req.query.page || 1
    const limit = +req.query.limit || 5
    const result = await UserService.getList(req.params, req.query, page, limit)
    res.send({
      ActionType: 'OK',
      data: result
    })
  },

  delList: async (req, res) => {
    // console.log(req.params.id)
    await UserService.delList({
      _id: req.params.id
    })
    res.send({
      ActionType: 'OK'
    })
  },

  putList: async (req, res) => {
    const result = await UserService.putList(req.body)
    res.send({
      ActionType: 'OK'
    })
  },

  getTeacherList: async (req, res) => {
    const result = await UserService.getTeacherList()
    res.send({
      ActionType: 'OK',
      data: result
    })
  },

  getStudentList: async (req, res) => {
    const page = +req.query.page || 1
    const limit = +req.query.limit || 5
    const result = await UserService.getStudentList(req.query, page, limit)
    res.send({
      ActionType: 'OK',
      data: result
    })
  },

  getTotal: async (req, res) => {
    const result = await UserService.getTotal(req.query)
    res.send({
      ActionType: 'OK',
      data: result
    })
  },

  getStudentTotal: async (req, res) => {
    const result = await UserService.getStudentTotal(req.query)
    res.send({
      ActionType: 'OK',
      data: result
    })
  }
}

module.exports = UserController
